<?php
	//$callback = "popupResult";
	
	$callback = Yii::app()->request->getParam("callback","");
//	echo "CALLBACK : ". Yii::app()->request->getParam("callback","");
?>
<script>
	var callbackFunct;
	function returnResultObject(val_id,val_name){
		var obj = {id:val_id,name:val_name};
		window.opener["<?php echo $callback ?>"](obj);
		window.close();
	}
 
	
</script> 
<?php
	//echo Yii::app()->request->getParam("departemen","dep kosong");
	
	$karyawans = CJSON::encode(MKaryawan::model()->findAll(array("order"=>"id")));	
?>

<script>
	//var karyawans = <?php //echo $karyawans?>;
	//var department_id = "<?php //echo $department_id ?>";		
	
	function reloadPage(){		
		$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid",[{current:true}]);
		$("#idSrcTblData").val("");
	}
	 
	
	$(function(){
		var departmentId= window.opener.$("#department_id").val();
		var numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number'};	 
		$("#tblData").jqGrid({
			url:'index.php?r=akMaster_Karyawan/detailList',
			editUrl:'index.php?r=akMaster_Karyawan/edit',
			postData:{"karyawan_id":1,"department_id":departmentId},
			datatype: 'json',
			loadonce:true,
			mtype:"GET",
			autowidth:true,
			height:"100%",
			altRows:true,
			ignoreCase:true,
			colNames: ['id','Nama Karyawan', 'Bagian','Action'],
			colModel: [
				{name: 'id', index: 'id', width: 40,hidden:false,sorttype:'int',formatter:'int'},
				{name: 'name', index: 'name', width: 40,search:true},
				{name: 'bagian', index: 'bagian'},
				{name: 'act', index: 'act', width: 40, align: 'center'},
				
			],
			gridview: true,
			rownumbers: false,
			rowNum: 10,
			rowList: [5, 10, 15],
			pager: '#navTblData',
			viewrecords: true,
			caption: 'Karyawan',
			jsonReader : { 
				root:"model",
				repeatitems: false			
			},
			loadComplete:function(data){
				console.log(data);
			},
			gridComplete: function(){ 
				var ids = jQuery("#tblData").getDataIDs(); 
				for(var i=0;i<ids.length;i++){ 
					var cl = ids[i]; 
					var cont = jQuery('#tblData').getCell(cl, 'name');					
					//btnEdit = "<input style='height:22px;width:20px;' type='button' value='E' onclick=jQuery('#rowed2').editRow("+cl+"); ></ids>"; 
					//btnDelete = "<input style='height:20px;width:20px;' type='button' value='S' onclick=jQuery('#rowed2').saveRow("+cl+"); />"; 
					btnEdit = "<img title='edit' class='imgBtn' style='height:20px;width:20px;' src='images/action/Sign-Select-icon.png' onclick='returnResultObject(" + cl + ",\"" + cont + "\")' />"; 
					jQuery("#tblData").setRowData(ids[i],{act:btnEdit}) 
				} 
			},
			ondblClickRow: function(){
				var row_id = $("#tblData").getGridParam('selrow');
				var cont = jQuery('#tblData').getCell(row_id, 'name');						 
					returnResultObject( row_id , cont );
			},
			 /*onSelectRow: function(id,status){
					var cont = jQuery('#tblData').getCell(id, 'name');						 
					returnResultObject( id , cont );
			 },*/  			
		});
		$("#tblData").jqGrid('navGrid','#navTblData',{add:false,edit:false,search:false,del:false,refresh:false});
		
		//add search text
		$("#tblData").jqGrid('addSearchText',["name","bagian"]);
	});
</script>

<div style="">
<script>
		$(function(){
			$("#department_id").change(function(){
				$("#tblData").jqGrid("setGridParam",{postData:{"department_id" :$("#department_id").val()},datatype:"json"}).trigger("reloadGrid");
			});
		});	
	</script>
	
	<div>
		<?php
			//$dep = Yii::app()->request->getParam("department","0");
			$department_ids= '<script language=javascript>document.write(window.opener.$("#department_id").val());</script>';			
			$rsDep = MDepartment::model()->findAll(array("condition"=>"is_active = TRUE","order"=>"id"));
			$arrOptions=array();
			foreach($rsDep as $rw){
				$arrOptions[] = array("label"=>$department_ids,"value"=>$department_ids);
			}
			$this->widget('application.components.widgets.SelectField', 
				array("name"=>"department_id",
					"value"=>"",
					"htmlOptions"=>"disabled",
					"label"=>array(
						"value"=>"Department",
						"htmlOptions"=>"class ='mandatory' style='display:inline-block;width:100px'"
					),
					"options"=>$arrOptions,
				)
			);
			
		?>		
	</div>
	<div style="clear:both"></div>	
	<table id="tblData"><tr></tr></table>
	<div id="navTblData"></div>
</div>